package com.infinite.itzone;

import android.os.Bundle;

import com.infinite.itzone.util.GMOnlineCustomerUtil;
import com.infinite.itzone.util.ShareUtils;

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {

  private static final String CUSTOMER_SHARE_SERVICE = "com.panda.customer.share";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

  }

  public void openQq(String qq){
    GMOnlineCustomerUtil.openQqCustomerServiceByQQNumber(MainActivity.this, qq);
  }

  // type 1-微博|2-朋友圈|3-微信|4-QQ|5-动弹|6-浏览器|7-复制|8-更多
  public void shareUrl(String url, int type){
    String pkg = null;
    String className = null;
    switch (type){
      case 1:
        pkg = "com.sina.weibo";
        className = "";
        break;
      case 2:
        pkg = "com.tencent.mm";
        className = "com.tencent.mm.ui.tools.ShareToTimeLineUI";
        break;
      case 3:
        pkg = "com.tencent.mm";
        className = "com.tencent.mm.ui.tools.ShareImgUI";
        break;
      case 4:
        pkg = "com.tencent.mobileqq";
        className = "com.tencent.mobileqq.activity.JumpActivity";
        break;
      case 5:
        break;

      default:
        break;
    }
    if(type == 8){
      ShareUtils.shareUrl(this, url);
    } else {
      ShareUtils.shareUrl(this, url, pkg, className);
    }
  }

  @Override
  public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
    GeneratedPluginRegistrant.registerWith(flutterEngine);


    //发送消息到flutter
    new EventChannel(flutterEngine.getDartExecutor(), CUSTOMER_SHARE_SERVICE).setStreamHandler(
            new EventChannel.StreamHandler() {
              @Override
              public void onListen(Object arguments, EventChannel.EventSink events) {
              }

              @Override
              public void onCancel(Object arguments) {
              }
            }
    );

    //从flutter接收消息
    new MethodChannel(flutterEngine.getDartExecutor(), CUSTOMER_SHARE_SERVICE).setMethodCallHandler(
            new MethodChannel.MethodCallHandler() {
              @Override
              public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                if (call.method.equals("openQq")) {
                  if(call.hasArgument("qq")) {
                    String qqNumber = call.argument("qq");
                    openQq(qqNumber);
                  }
                } else if(call.method.equals("shareUrl")){
                  int type = 0;
                  String url = null;
                  if(call.hasArgument("type") && call.argument("type") != null){
                    type = call.argument("type");
                  }
                  if(call.hasArgument("url")){
                    url = call.argument("url");
                  }
                  shareUrl(url, type);
                } else {
                  result.notImplemented();
                }
              }
            }
    );
  }
}
